package com.evaluation.kexingcp.pay.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.evaluation.kexingcp.common.core.util.R;
import com.evaluation.kexingcp.common.excel.annotation.ResponseExcel;
import com.evaluation.kexingcp.common.log.annotation.SysLog;
import com.evaluation.kexingcp.common.security.annotation.Inner;
import com.evaluation.kexingcp.pay.entity.PayGoodsEntity;
import com.evaluation.kexingcp.pay.service.PayGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * ${tableComment}
 *
 * @author kexingcp
 * @date 2025-02-19 14:51:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/payGoods" )
@Tag(description = "payGoods" , name = "${tableComment}管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PayGoodsController {

    private final PayGoodsService payGoodsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param payGoods ${tableComment}
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_payGoods_view')" )
    public R getPayGoodsPage(@ParameterObject Page page, @ParameterObject PayGoodsEntity payGoods) {
        LambdaQueryWrapper<PayGoodsEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(payGoodsService.page(page, wrapper));
    }


    @GetMapping("/list" )
    @Inner(value = false)
    public R list() {
        LambdaQueryWrapper<PayGoodsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PayGoodsEntity::getGoodsType, 1);
        wrapper.orderByAsc(PayGoodsEntity::getGoodsSort);

        return R.ok(payGoodsService.list(wrapper));
    }


    /**
     * 通过id查询${tableComment}
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_payGoods_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(payGoodsService.getById(id));
    }

    /**
     * 新增${tableComment}
     * @param payGoods ${tableComment}
     * @return R
     */
    @Operation(summary = "新增${tableComment}" , description = "新增${tableComment}" )
    @SysLog("新增${tableComment}" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_payGoods_add')" )
    public R save(@RequestBody PayGoodsEntity payGoods) {
        return R.ok(payGoodsService.save(payGoods));
    }

    /**
     * 修改${tableComment}
     * @param payGoods ${tableComment}
     * @return R
     */
    @Operation(summary = "修改${tableComment}" , description = "修改${tableComment}" )
    @SysLog("修改${tableComment}" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_payGoods_edit')" )
    public R updateById(@RequestBody PayGoodsEntity payGoods) {
        return R.ok(payGoodsService.updateById(payGoods));
    }

    /**
     * 通过id删除${tableComment}
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除${tableComment}" , description = "通过id删除${tableComment}" )
    @SysLog("通过id删除${tableComment}" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin_payGoods_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(payGoodsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param payGoods 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('admin_payGoods_export')" )
    public List<PayGoodsEntity> export(PayGoodsEntity payGoods,Long[] ids) {
        return payGoodsService.list(Wrappers.lambdaQuery(payGoods).in(ArrayUtil.isNotEmpty(ids), PayGoodsEntity::getId, ids));
    }
}